# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time

from openerp.osv import fields, osv
from openerp.tools.translate import _

class edit_qty_so_line(osv.osv_memory):
    _name = 'edit.qty.so.line'
    _description = 'Edit Qty SO Line'

    def default_get(self, cr, uid, fields, context=None):
#        raise osv.except_osv(_('Error !'), _('This button still in progress mode'))
        if context is None:
            context = {}
        sale_order_line_obj = self.pool.get('sale.order.line')
        stock_move_obj = self.pool.get('stock.move')
        product_uom_obj = self.pool.get('product.uom')
        res = super(edit_qty_so_line, self).default_get(cr, uid, fields, context=context)
        for lines in sale_order_line_obj.browse(cr, uid, context.get(('active_ids'), []), context=context):
            qty_delivery = 0.00
            proc_ids = []
            for proc in lines.procurement_ids:
                proc_ids.append(proc.id)
            move_ids = stock_move_obj.search(cr, uid, [('procurement_id','in',proc_ids),('state','!=','cancel')])
            if move_ids:
                for mv in stock_move_obj.browse(cr, uid, move_ids, context=context):

                    qty_delivery = qty_delivery +  mv.product_uom_qty

            if 'qty_order' in fields:
                res.update({'qty_order': lines.product_uom_qty})
            if 'qty_delivery' in fields:
                res.update({'qty_delivery': qty_delivery})
            if 'qty_remaining' in fields:
                res.update({'qty_remaining': lines.product_uom_qty - qty_delivery})
 
        return res

    def change_qty(self, cr, uid, ids, context=None):
        sale_order_line_obj = self.pool.get('sale.order.line')
        for obj in self.browse(cr, uid, ids, context=context):
            if obj.product_uom_qty <= 0:
                raise osv.except_osv(_('Error !'), _('The Quantity Entered cannot same or less than 0'))
            if obj.product_uom_qty < obj.qty_delivery:
                raise osv.except_osv(_('Error !'), _('The Quantity Entered cannot less than the Quantity Delivery'))
            sale_order_line_obj.write(cr, uid, context.get(('active_ids'), []), {'product_uom_qty': obj.product_uom_qty}, context=context)
        return {'type': 'ir.actions.act_window_close'}

    _columns = {
        'qty_order': fields.float('Qty Order', readonly=True),
        'qty_delivery': fields.float('Qty Delivery', readonly=True),
        'qty_remaining': fields.float('Qty Remaining', readonly=True),
        'product_uom_qty': fields.float('Quantity (UoM)', required=True),
    }

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
